/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.admin.controller;

// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
// import com.esotericsoftware.minlog.Log;
import com.yami.shop.bean.model.Article;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.admin.util.SecurityUtils;
import com.yami.shop.service.ArticleService;
import lombok.AllArgsConstructor;
import com.yami.shop.common.response.ServerResponseEntity;

// import org.hibernate.validator.internal.util.logging.Log_.logger;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.Date;

import lombok.extern.slf4j.Slf4j;

/**
 * 公告管理
 *
 * @author hzm
 * @date
 */
@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("/shop/article")
public class ArticleController {

    // private final Logger logger = LoggerFactory.getLogger(ArticleController.class);
    
    private final ArticleService articleService;

    /**
     * 分页查询
     *
     * @param page   分页对象
     * @param article 公告管理
     * @return 分页数据
     */
    @GetMapping("/page") 
    public ServerResponseEntity<IPage<Article>> getArticlePage(PageParam<Article> page, Article article) {

        log.info("分页查询article.getTitle：{}", article.getTitle()); //是url里的参数
        // log.info("Article::getTitle：{}", Article::getTitle);
        
        IPage<Article> articlePage = articleService.page(page, new LambdaQueryWrapper<Article>()
                .eq(article.getStatus() != null, Article::getStatus, article.getStatus())
                .eq(article.getIsTop()!=null,Article::getIsTop,article.getIsTop())
                // .like(article.getTitle() != null, Article::getTitle, article.getTitle())
                .nested(article.getTitle() != null, 
                    wrapper -> wrapper
                        .like(Article::getTitle, article.getTitle())
                        .or()
                        .like(Article::getContent, article.getTitle())
                )
                .orderByDesc(Article::getUpdateTime));
        return ServerResponseEntity.success(articlePage);
    }


    /**
     * 通过id查询公告管理
     *
     * @param id id
     * @return 单个数据
     */
    @GetMapping("/info/{id}")
    public ServerResponseEntity<Article> getById(@PathVariable("id") Long id) {
        return ServerResponseEntity.success(articleService.getById(id));
    }

    /**
     * 新增公告管理
     *
     * @param article 公告管理
     * @return 是否新增成功
     */
    @SysLog("新增公告管理")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('shop:article:save')")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid Article article) {
        article.setShopId(SecurityUtils.getSysUser().getShopId());
        if (article.getStatus() == 1) {
            article.setPublishTime(new Date());
        }
        article.setUpdateTime(new Date());
        articleService.removeArticleList();
        return ServerResponseEntity.success(articleService.save(article));
    }

    /**
     * 修改公告管理
     *
     * @param article 公告管理
     * @return 是否修改成功
     */
    @SysLog("修改公告管理")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('shop:article:update')")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid Article article) {
        Article oldArticle = articleService.getById(article.getId());
        if (oldArticle.getStatus() == 0 && article.getStatus() == 1) {
            article.setPublishTime(new Date());
        }
        // logger.info("修改公告管理：{}", article);
        article.setUpdateTime(new Date());
        articleService.removeArticleList();
        articleService.removeArticleById(article.getId());
        return ServerResponseEntity.success(articleService.updateById(article));
    }

    /**
     * 通过id删除公告管理
     *
     * @param id id
     * @return 是否删除成功
     */
    @SysLog("删除公告管理")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('shop:article:delete')")
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long id) {
        articleService.removeArticleList();
        articleService.removeArticleById(id);
        return ServerResponseEntity.success(articleService.removeById(id));
    }

}
